﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UBConDemo.Models;
using System.Collections.ObjectModel;
using System.Reflection;
using System.IO;
using System.Xml;
using System.Windows.Media;
using System.Windows.Media.Animation;
using UBCon.Core;
using System.Windows;

namespace UBConDemo.ViewModels
{
    public class UBConShellViewModel:ViewModelBase
    {
        //------------------------------------------------------
        //
        //  Constructors
        //
        //------------------------------------------------------
        #region Constructors

        /// <summary>
        /// Initializes a new instance of UBConDemo.ViewModels.UBConShellViewModel class
        /// </summary>
        public UBConShellViewModel() 
        {
            LoadData();
        }
        #endregion

        //------------------------------------------------------
        //
        //  Properties
        //
        //------------------------------------------------------
        #region Properties

        /// <summary>
        /// Gets or sets a collection of UbConItemModel class.
        /// </summary>
        public ObservableCollection<UbConItemModel> Items 
        {
            get 
            {
                if (_items == null) 
                {
                    Items = new ObservableCollection<UbConItemModel>();
                }
                return _items;
            }
            set 
            {
                bool _shouldRaiseEvent = !ReferenceEquals(_items, value);
                _items = value;
                if (_shouldRaiseEvent) 
                {
                    OnPropertyChanged("Items");
                }
            }
        }

        /// <summary>
        /// Gets or sets selected item.
        /// </summary>
        public UbConItemModel SelectedItem 
        {
            get 
            {
                return _selectedItem;
            }
            set 
            {
                bool _shouldRaiseEvent = !ReferenceEquals(_selectedItem, value);
                _selectedItem = value;
                if (_shouldRaiseEvent) 
                {
                    OnPropertyChanged("SelectedItem");
                }
            }
        }

        #endregion

        //------------------------------------------------------
        //
        //  Depenency Property Changed Callbacks
        //
        //------------------------------------------------------
        #region Depenency Property Changed Callbacks
        #endregion

        //------------------------------------------------------
        //
        //  Overrides
        //
        //------------------------------------------------------
        #region Overrides

        #endregion

        //------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------
        #region Public Methods
        #endregion

        //------------------------------------------------------
        //
        //  Internal Methods
        //
        //------------------------------------------------------
        #region Internal Methods
        #endregion

        //------------------------------------------------------
        //
        //  Private Methods
        //
        //------------------------------------------------------
        #region Private Methods

        /// <summary>
        /// Loads data from UBConDemoMetadata.xml file.
        /// </summary>
        private void LoadData() 
        {
            XmlDocument doc = new XmlDocument();
            XmlNode mainNode = null, childNode = null;
            using (Stream stream = (Stream)Assembly.GetExecutingAssembly().GetManifestResourceStream("UBConDemo.UBConDemoMetadata.xml")) 
            {
                doc.Load(stream);
                mainNode = doc.ChildNodes[1];
                Title = mainNode.Attributes["Title"].InnerText;
                int count = mainNode.ChildNodes.Count;

                for (int i = 0; i < count; i++)
                {
                    childNode = mainNode.ChildNodes[i];
                    Items.Add
                        (
                            new UbConItemModel()
                            {
                                IsCategoryNameItem = true,
                                Title = childNode.Attributes["Title"].InnerText
                            }
                        );
                    for (int j = 0; j < childNode.ChildNodes.Count; j++)
                    {
                        XmlNode innerNode = childNode.ChildNodes[j];
                        Items.Add
                        (
                            new UbConItemModel()
                            {
                                Title = innerNode.Attributes["Title"].InnerText,
                                Image = innerNode.Attributes["Image"].InnerText,
                                View = innerNode.Attributes["View"].InnerText,
                                IsEnabled = innerNode.Attributes["IsEnabled"].InnerText == "True" ? true : false,
                                ViewType = innerNode.Attributes["ViewType"].InnerText,
                                ShowInAnotherWindow = innerNode.Attributes["ShowInAnotherWindow"].InnerText == "True" ? true : false
                            }
                        );
                    }
                }
            }
            
        }

     
        #endregion

        //------------------------------------------------------
        //
        //  Event Handlers
        //
        //------------------------------------------------------
        #region Event Handlers
        #endregion

        //------------------------------------------------------
        //
        //  Fields
        //
        //------------------------------------------------------
        #region Fields

        private ObservableCollection<UbConItemModel> _items = null;
        private UbConItemModel _selectedItem = null;
        
        #endregion

        //------------------------------------------------------
        //
        //  Events
        //
        //------------------------------------------------------
        #region Events
        #endregion

        //------------------------------------------------------
        //
        //  Commands
        //
        //------------------------------------------------------
        #region Commands
        #endregion

    }
}
